package com.wyl.schedule;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;

import com.wyl.model.Simple;
import com.wyl.service.DetailService;
import com.wyl.service.SimpleService;
import com.wyl.spider.pipline.DetailPipeline;
import com.wyl.spider.pipline.SimplePipeline;
import com.wyl.spider.processor.DetailProcessor;
import com.wyl.spider.processor.SimpleProcessor;
import com.wyl.utils.Utils;

import us.codecraft.webmagic.Spider;

@Configuration
@EnableScheduling // 启用定时任务
public class ScheduleTask {

	private Logger logger = LoggerFactory.getLogger(ScheduleTask.class);

	@Autowired
	private SimplePipeline simplePipeline;
	@Autowired
	private DetailPipeline detailPipeline ;
	@Autowired
	private SimpleService simpleService;
	@Autowired
	private DetailService detailService;
	
//	@Scheduled(cron = "*/5 * * * * ?")//每10分钟执行一次
//	public void test(){
//		logger.info(new Date().toString());
//	}

	@Scheduled(cron = "0 0/3 * * * ?")//每3分钟执行一次
	public void simple(){
		
		checkAndClear();
		
		Set<String> keys = Utils.getUrlMap().keySet();
		List<String> urlList = new ArrayList<String>(keys);
		
		Spider spider = Spider.create(new SimpleProcessor());
		spider.startUrls(urlList);
		spider.addPipeline(simplePipeline).thread(1).run();
		logger.info("SimpleTask run at:"+new Date());
		this.detail();
	}
	
	public void detail(){
		Spider spider = Spider.create(new DetailProcessor());
		
		List<Simple> ls = simpleService.selectNoContent();
		List<String> startUrls = new ArrayList<String>();
		for (Simple simple : ls) {
			startUrls.add(simple.getDocurl());
		}		
		spider.startUrls(startUrls);
		spider.addPipeline(detailPipeline).thread(1).run();
	}
	
	@Transactional
	public void checkAndClear(){
		List<Simple>ls= simpleService.selectAll();
		if(ls.size()>5000){
			simpleService.clearTable();
			detailService.clearTable();
		}
		logger.info("simple number :"+ls.size());
	}
	
	
}
